package cn.tx.redislock.service;

import cn.tx.redislock.mylock.DistributedLockFactory;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author jizenan
 * @date 2025/6/6
 * Description:
 */
@Service
@Slf4j
public class InventoryService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Value("${server.port}")
    private String port;

    private Lock lock = new ReentrantLock();

    //private Lock redisDistributedLock = new RedisDistributedLock();

    @Autowired
    private DistributedLockFactory factory;

    @Autowired
    private Redisson redisson;


    /**
     * V9.0，引入redisson官网推荐RedLock算法实现类
     * */
    public String saleByRedisson() {
        String retMessage = "";

        RLock redissonLock = redisson.getLock("zzyyRedisLock");
        redissonLock.lock();
        try {
            //1 查询库存信息
            String result = stringRedisTemplate.opsForValue().get("inventory001");
            //2 判断库存是否足够
            Integer inventoryNumber = result == null ? 0 : Integer.parseInt(result);
            //3 扣减库存，每次减少一个
            if(inventoryNumber > 0){
                stringRedisTemplate.opsForValue().set("inventory001",String.valueOf(--inventoryNumber));
                retMessage = "成功卖出一个商品，库存剩余："+inventoryNumber;
                System.out.println(retMessage+"\t"+"服务端口号"+port);
            }else {
                retMessage = "商品卖完了.....";
            }
        } finally {
            //改进点，只能删除自己的key，不能删除别人的
            if(redissonLock.isLocked() && redissonLock.isHeldByCurrentThread()){
                redissonLock.unlock();
            }
        }
        return retMessage+"\t"+"服务端口号"+port;
    }

    public String sale() {
        String retMessage = "";
        lock.lock();
        try {
            //1 查询库存信息
            String result = stringRedisTemplate.opsForValue().get("inventory001");
            //2 判断库存是否足够
            Integer inventoryNumber = result == null ? 0 : Integer.parseInt(result);
            //3 扣减库存，每次减少一个
            if(inventoryNumber > 0){
                stringRedisTemplate.opsForValue().set("inventory001",String.valueOf(--inventoryNumber));
                retMessage = "成功卖出一个商品，库存剩余："+inventoryNumber;
                System.out.println(retMessage+"\t"+"服务端口号"+port);
            }else {
                retMessage = "商品卖完了.....";
            }
        } finally {
            lock.unlock();
        }
        return retMessage+"\t"+"服务端口号"+port;
    }
}
